$(function () {

    let channel = getQueryString("channel");
    let url = '../user/list';
    if (channel) {
        url += '?channel=' + channel;
    }

    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', align: "center", name: 'id', index: 'id', key: true, hidden: true},
            {
                label: '昵称', align: "center", name: 'nickname', index: 'nickname', width: 80
                , formatter: nameLink,
                editable: true
            },
            {
                label: '手机', align: "center", name: 'mobile', index: 'mobile', width: 80
                , formatter: mobileLink,
                editable: true
            },
            {label: '充值总额/元', align: "center", name: 'czAmount', index: 'czAmount', width: 80},
            {label: '产品消费金额/元', align: "center", name: 'xsAmount', index: 'xsAmount', width: 100},
            {label: '视频聊天时长/分钟', align: "center", name: 'talkTimes', index: 'talkTimes', width: 100},
            {
                label: '性别', align: "center", name: 'gender', index: 'gender', width: 80,
                formatter: function (value) {
                    return transGender(value);
                }
            },
            {
                label: '注册时间', align: "center", name: 'registerTime', index: 'register_time', width: 120,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '近一次登录时间',
                name: 'lastLoginTime',
                align: "center",
                index: 'last_login_time',
                width: 120,
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            },
            {
                label: '头像', align: "center", name: 'avatar', index: 'avatar', width: 80, formatter: function (value) {
                    return transImg(value);
                }
            },
            {
                label: '状态', name: 'status', index: 'status', width: 80, align: "center"
                , formatter: function (value) {
                    return transStatus(value);
                }
            }
        ],
        viewrecords: true,
        height:
            385,
        rowNum:
            10,
        rowList:
            [10, 30, 50],
        rownumbers:
            true,
        rownumWidth:
            25,
        autowidth:
            true,
        multiselect:
            true,
        cz_amount:
            0,
        xf_amount:
            0,
        yaoqingCount:
            0,
        orderCount:
            0,
        pager:
            "#jqGridPager",
        jsonReader:
            {
                root: "page.list",
                page:
                    "page.currPage",
                total:
                    "page.totalPage",
                records:
                    "page.totalCount"
            }
        ,
        prmNames: {
            page: "page",
            rows:
                "limit",
            order:
                "order"
        }
        ,
        postData: {
            'identity':
                1
        }
        ,
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    })
    ;
});

function nameLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.nickname + '</a>';
}

function mobileLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.mobile + '</a>';
}

function getinfo(id) {
    vm.showList = false;
    vm.title = "修改";
    vm.cz_amount = 0;
    vm.xf_amount = 0;
    vm.yaoqingCount = 0;
    vm.orderCount = 0;
    vm.phone = "";
    vm.getInfo(id);
}


let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        cz_amount: 0,
        xf_amount: 0,
        phone: '',
        yaoqingCount: 0,
        orderCount: 0,
        apiUser: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.apiUser = {};
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.cz_amount = 0;
            vm.xf_amount = 0;
            vm.yaoqingCount = 0;
            vm.orderCount = 0;
            vm.phone = "";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.apiUser.id == null ? "../user/save" : "../user/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.apiUser),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../user/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        /**
         * 全部评论
         */
        getComments: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '个人评论统计',
                type: 2,
                content: '../order/comment.html?userId=' + id
            })
        },
        /**
         * 探索点赞统计
         */
        getCircle: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '个人探索点赞统计',
                type: 2,
                content: '../extend/circle.html?userId=' + id
            })
        },
        /**
         * 视频聊天统计
         */
        getCallBill: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '个人视频聊天统计',
                type: 2,
                content: '../extend/callBIll.html?userId=' + id
            })
        },
        /**
         * 订单统计
         */
        getOrderBill: function () {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            openWindow({
                title: '个人订单统计',
                type: 2,
                content: '../extend/orderBill.html?userId=' + id
            })
        },
        getInfo: function (id) {
            $.get("../user/info/" + id, function (r) {
                vm.apiUser = r.apiUser;
                vm.cz_amount = r.cz_amount;
                vm.xf_amount = r.xf_amount;
                vm.yaoqingCount = r.yaoqingCount;
                vm.orderCount = r.orderCount;
                vm.phone = r.phone;
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'name': vm.q.name
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});